﻿package makeeasier.utils {
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.utils.*;
	
	public class FPSMeter extends Sprite {
		
		public static var FPS_UPDATE:String = "fpsUpdate";
		private var _fps:Number;
		private var _lastFPS:Number;
		private var _lastState:Number;
		private var _stopped:Boolean;
		private var _state:Boolean;
		
		public function FPSMeter(){
			_stopped = false;
			_fps = 0;
			_lastFPS = 0;
			_lastState = -2000;
			_state = false;
		}
		
		private function run():void{
			//trace("sdf");
			if(getTimer() - _lastState < 1000){
				_fps+=1;
			} else {
				_lastFPS = _fps;
				_fps = 0;
				dispatchEvent(new Event(FPS_UPDATE));
				_lastState = getTimer();
			}
			if(!_stopped){
				setTimeout(run,1);
				//run();
			}
			/*
			_state = true;
			while( _state ){
				_fps+=0.1;
				//trace(_fps);
				_state = getTimer() - _lastState > 100 ? false : true;
			}
			_lastFPS = _fps / 10;
			_fps = 0;
			dispatchEvent(new Event(FPS_UPDATE));
			//trace(_lastState + " / " + getTimer());
			_lastState = getTimer();
			if(!_stopped){
				setTimeout(run,50);
				//run();
			}
			*/
		}
		
		public function start():void{
			_stopped = false;
			run();
		}
		
		public function stop():void{
			_stopped = true;
		}
		
		public function get fps():Number{
			return _lastFPS;
		}
		
		
	}
	
}